package com.vodone.v1pin.component.linklabel
{
	import flash.events.MouseEvent;
	import flash.ui.ContextMenu;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	import mx.controls.Label;
	import mx.core.UITextField;
	import mx.events.FlexEvent;
	
	
	/**
	 * 鼠标移过的颜色
	 */
	[Style(name="overColor", type="Number", format="uint", inherit="no")]
	/**
	 * 正常颜色
	 */
	[Style(name="normalColor", type="Number", format="uint", inherit="no")]
	/**
	 * 是否over的时候出现下划线
	 */
	[Style(name="overUnderline", type="String", inherit="no")]
	/**
	 * 鼠标移上去变色的Label
	 */
	public class LinkLabel extends Label
	{
		/**
		 * 与label对应的id
		 */
		public var labelID:String;
		
		public function LinkLabel()
		{
			this.addEventListener(FlexEvent.CREATION_COMPLETE, init);
			this.selectable=false;
		}
		
		override public function set text(value:String):void
		{
			super.text=value;
		}
		
		public override function set enabled(value:Boolean):void
		{
			super.enabled=value;
		}
		
		private var _selected:Boolean = false;
		[Bindable]
		public function get selected():Boolean
		{
			return _selected;
		}
		public function set selected( value:Boolean ):void
		{
			if( _selected != value )
			{
				_selected = value;
				invalidCurrentColor();
			}
		}
		
		private function invalidCurrentColor():void
		{
			_currentColorChanged = true;
			this.invalidateDisplayList();
		}
		private function commitCurrentColor():void
		{
			if( _selected )
			{
				currentColor = _selectedColor;
			}
			else
			{
				currentColor = _normalColor;
			}
		}
		
		private function init(e:FlexEvent):void
		{
			setContextMenu();
			initListeners();
			styleChanged("normalColor");
			styleChanged("overColor");
			invalidCurrentColor();
			setStyle( "color" , _normalColor );
		}
		
		private function initListeners():void
		{
			addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			addEventListener(MouseEvent.MOUSE_OUT, outHandler);
			addEventListener(MouseEvent.CLICK, clickHandler, true);
			addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			addEventListener(MouseEvent.MOUSE_UP, upHandler);
			addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
		}
		
		private var _overColor:int = 0x000000;
		private var _normalColor:int = 0x666666;
		private var _showUnderLine:Boolean = false;
		private var _selectedColor:int = 0x000000;
		
		private var _overColorChanged:Boolean = false;
		private var _normalColorChanged:Boolean = false;
		private var _selectedColorChanged:Boolean = false;
		
		private var _currentColor:int = _normalColor;
		private var _currentColorChanged:Boolean = false;
		[Bindable]
		public function get currentColor():int
		{
			return _currentColor;
		}
		public function set currentColor( value:int ):void
		{
			_currentColor = value;
			this.setStyle("color",value);
		}
		
		private var mouseIsDown:Boolean=false;
		private var mouseIsMoveAfterDown:Boolean=false;
		
		override protected function createChildren():void
		{
			super.createChildren();
		}
		
		override public function styleChanged(styleProp:String):void
		{
			super.styleChanged(styleProp);
			
			switch (styleProp)
			{
				case "overColor":
					_overColorChanged = true;
					break;
				case "normalColor":
					_normalColorChanged = true;
					break;
				case "overUnderline":
					var showUnderline:String=this.getStyle("overUnderline") as String;
					if (showUnderline == "true")
					{
						_showUnderLine=true;
					}
					if (showUnderline == "false")
					{
						_showUnderLine=false;
					}
					break;
			}
			this.invalidateDisplayList();
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if (_normalColorChanged && getStyle("normalColor") != null )
			{
				_normalColor = getStyle("normalColor") as int;
				currentColor = _normalColor;
				_normalColorChanged = false;
			}
			if ( _overColorChanged && getStyle("overColor") != null )
			{
				_overColor = getStyle("overColor") as int;
				_selectedColor = _overColor;
				_overColorChanged = false;
			}
			
			if( _currentColorChanged )
			{
				commitCurrentColor();
				_currentColorChanged = false;
			}
		}
		
		private function setContextMenu():void
		{
			var cm:ContextMenu=new ContextMenu();
			cm.hideBuiltInItems();
			cm.clipboardMenu=true;
			UITextField(textField).contextMenu=cm;
		}
		
		// ============================== 鼠标处理 start  =============================================== //
		private function overHandler(e:MouseEvent):void
		{
			setHandCursor();
			currentColor=_overColor;
			if (_showUnderLine)
			{
				this.setStyle("textDecoration", "underline");
			}
			this.invalidateDisplayList();
		}
		
		/**
		 * 设置鼠标显示为手状
		 * */
		private function setHandCursor():void
		{
			if(enabled)
				Mouse.cursor = MouseCursor.BUTTON;
		}
		
		/**
		 * 移除手状鼠标 重设为箭头样式
		 * */
		private function removeHandCursor():void
		{
			if(enabled)
				Mouse.cursor = MouseCursor.AUTO;
		}
		
		private function moveHandler(e:MouseEvent):void
		{
			if (mouseIsDown)
			{
				mouseIsMoveAfterDown=true;
			}
		}
		
		private function upHandler(e:MouseEvent):void
		{
			mouseIsDown=false;
		}
		
		private function downHandler(e:MouseEvent):void
		{
			mouseIsDown=true;
			mouseIsMoveAfterDown=false;
		}
		
		private function clickHandler(e:MouseEvent):void
		{
			if (mouseIsMoveAfterDown)
			{
				e.stopImmediatePropagation();
			}
		}
		
		private function outHandler(e:MouseEvent):void
		{
			removeHandCursor();
			mouseIsDown=false;
			mouseIsMoveAfterDown=false;
			invalidCurrentColor();
			if (_showUnderLine)
			{
				this.setStyle("textDecoration", "none");
			}
			this.invalidateDisplayList();
		}
		
		// ============================== 鼠标处理 end  =============================================== //
	}
}